package com.itboye.sunsun.me.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.itboye.homesensen.alert.ByAlert;
import com.itboye.sunsunhome.R;
import com.itboye.sunsun.adapter.MeMyTieziAdaptyer;
import com.itboye.sunsun.adapter.WoDeHuiTieAdapter;
import com.itboye.sunsun.app.swipemenulistview.SwipeMenu;
import com.itboye.sunsun.app.swipemenulistview.SwipeMenuCreator;
import com.itboye.sunsun.app.swipemenulistview.SwipeMenuItem;
import com.itboye.sunsun.app.swipemenulistview.SwipeMenuListView;
import com.itboye.sunsun.app.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.MySunSunTieziHuiBean;
import com.itboye.sunsun.beans.MySunSunTieziHuiBean.MyTieziHuifu;
import com.itboye.sunsun.beans.MyTieZiMeBean;
import com.itboye.sunsun.beans.MyTieZiMeBean.TieZibean;
import com.itboye.sunsun.beans.WoDeHuiTieBean;
import com.itboye.sunsun.beans.WoDeHuiTieBean.WoDeHuiTieItemBean;
import com.itboye.sunsun.beans.WoDeZhuTiBean.ZhuTiItemBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.custome.XListView.IXListViewListener;
import com.itboye.sunsun.luntan.TieZiDetailActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.uesrinfo.TieZiQuanXianManager;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;

public class MyTieziFragment extends BaseFragment {

	private SwipeMenuListView xlistview;
	private int pageNum = 1;

	private BaseAdapter adapter;
	List<TieZibean> dataList = new ArrayList<>();
	
	String tid;
	
	private LinearLayout produceterLiner;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater
				.inflate(R.layout.fragment_wo_de_tiezi, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		xlistview.setPullRefreshEnable(true);
		xlistview.setPullLoadEnable(true);
		xlistview.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				pullDown();
			}

			@Override
			public void onLoadMore() {
				pullUp();
			}

		});

		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				// TODO Auto-generated method stub
				SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity());
				// set item background
				deleteItem.setBackground(new ColorDrawable(getResources().getColor(R.color.home_blue)));
				deleteItem.setTitle("删除");
				deleteItem.setTitleColor(Color.WHITE);
				// // set item width
				deleteItem.setWidth(dp2px(90));
				deleteItem.setTitleSize(18);
				// // set a icon
				// deleteItem.setIcon(R.drawable.ic_delete);
				// deleteItem.setBackground(R.drawable.delete_list_item_bg);
				// add to menu
				menu.addMenuItem(deleteItem);
			}
		};

		xlistview.setMenuCreator(creator);
		xlistview.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(  int position, SwipeMenu menu, int index) {

				switch (index) {
				case 0:
					tid=dataList.get(position).getTid();
					publicDelete(tid);
					break;
				
				}
			}
		});
	
		adapter = new MeMyTieziAdaptyer(dataList);
		xlistview.setAdapter(adapter);
	
	
		pullDown();

		xlistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				 Intent intent=new Intent(getActivity(),
				 TieZiDetailActivity.class);
				 TieZibean bean=dataList.get(position-1);
				 intent.putExtra("fid", bean.getFid());
				 intent.putExtra("tid", bean.getTid());
				
				 intent.putExtra("author", bean.getAuthor());
				 intent.putExtra("author_id", bean.getAuthor_id());
				
				 startActivity(intent);
			}
		});

	}

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}

	private void pullDown() {

		pageNum = 1;
		MyJsonRequest.Builder<MyTieZiMeBean> builder = new MyJsonRequest.Builder<MyTieZiMeBean>()
				.param("uid",(String) SPUtils.get(getContext(), null,SPContants.USER_ID, ""))
				.apiVer("100")
				.param("page_no", "" + pageNum)
				.typeKey("BY_BbsUserCenter_trueQueryThread")
				.requestListener(new XRequestListener<MyTieZiMeBean>() {

					@Override
					public void onResponse(MyTieZiMeBean bean) {
						// onPullDownFinish(adapter, xlistview, bean);
						pageNum++;
						xlistview.stopRefresh();
						dataList.clear();
						if (bean.getList() != null) {
							dataList.addAll(bean.getList());
							adapter.notifyDataSetChanged();
						}else if(bean.getList()==null){
							produceterLiner.setVisibility(View.VISIBLE);
							xlistview.setVisibility(View.GONE);
						}

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						xlistview.stopRefresh();
						// onPullDownError(adapter, xlistview, exception, code,
						// msg);
					}
				});

		MyJsonRequest<MyTieZiMeBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void pullUp() {
		MyJsonRequest.Builder<MyTieZiMeBean> builder = new MyJsonRequest.Builder<MyTieZiMeBean>()
				.param("uid",(String) SPUtils.get(App.ctx, null, SPContants.USER_ID,""))
				.apiVer("100")
				.typeKey("BY_BbsUserCenter_trueQueryThread")
				.param("page_no", "" + pageNum)

				.requestListener(new XRequestListener<MyTieZiMeBean>() {

					@Override
					public void onResponse(MyTieZiMeBean bean) {
						// onPullUpFinish(adapter, xlistview, bean);
						pageNum++;
						xlistview.stopLoadMore();
						if (bean.getList() != null) {
							dataList.addAll(bean.getList());
							adapter.notifyDataSetChanged();
						}
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						xlistview.stopLoadMore();
						// onPullUpError(adapter, xlistview, exception, code,
						// msg);
					}
				});

		MyJsonRequest<MyTieZiMeBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void publicDelete(final String tid) {
		pageNum = 1;
		MyJsonRequest.Builder<String> builder = new MyJsonRequest.Builder<String>()
				.param("uid",(String) SPUtils.get(getContext(), null,SPContants.USER_ID, "")).apiVer("100")
				.param("tid",tid)
				.typeKey("BY_ForumThread_delete")
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String bean) {
						Iterator<TieZibean>iterator=dataList.iterator();
						while (iterator.hasNext()) {
							TieZibean bean2=iterator.next();
							if(bean2.getTid().equals(tid)){
								iterator.remove();
								break;
							}
						}
//						ByAlert.alert("删除成功!");
//					
//						pullDown();
						adapter.notifyDataSetChanged();
						
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						// onPullDownError(adapter, xlistview, exception, code,
						// msg);
					}
				});

		MyJsonRequest<String> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}
}
